home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / Player.aspx.cs175 < prev    next >
Text File  |  2007-11-24  |  45KB  |  1,058 lines

  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Net;
  6. using System.Web;
  7. using System.Web.UI;
  8. using GBPVR.Public;
  9. using GBPVRSchedule;
  10. using gbweb.classes;
  11.  
  12. namespace gbweb
  13. {
  14.     public partial class Player : Page
  15.     {
  16.         private Settings guideParams;
  17.         private string serverAddr;
  18.         private Process proc;
  19.         private ProcessStartInfo psi;
  20.  
  21.         protected void Page_Load(object sender, EventArgs e)
  22.         {
  23.  
  24.             getTheme();
  25.             if (!IsPostBack)
  26.             {
  27.                 //Kill any running VLC processes
  28.                 killVLC();
  29.  
  30.                 playerOptions.Visible = true;
  31.                 transConfigOptions.Visible = true;
  32.                 transcodeOptions.Visible = false;
  33.                 controlTable.Visible = false;
  34.                 buttonTable.Visible = false;
  35.  
  36.                 resetButton_Click(null, null);
  37.             }
  38.         }
  39.  
  40.         private void AddKeepAlive()
  41.         {
  42.             //Routine to keep session alive
  43.             int int_MilliSecondsTimeOut = (Session.Timeout * 60000) - 30000;
  44.             string str_Script = @"
  45.                 <script type='text/javascript'>
  46.                 //Number of Reconnects
  47.                 var count=0;
  48.                 //Maximum reconnects setting
  49.                 var max = 5;
  50.                 window.status = 'Auto Reconnect Script is Activated'
  51.                 function Reconnect(){
  52.                 count++;
  53.                 if (count < max)
  54.                 {
  55.                 window.status = 'Link to Server Refreshed ' + count.toString()+' time(s)' ;
  56.  
  57.                 var img = new Image(1,1);
  58.  
  59.                 img.src = 'keepalive.aspx';
  60.  
  61.                 }
  62.                 }
  63.  
  64.                 window.setInterval('Reconnect()'," + int_MilliSecondsTimeOut.ToString() + @"); //Set to length required
  65.  
  66.                 </script>
  67.  
  68.                 ";
  69.  
  70.             ClientScript.RegisterStartupScript(typeof(String), "Reconnect", str_Script, false);
  71.         }
  72.         
  73.         private void AddPlayerScript()
  74.         {
  75.             //Insert script to the client to play the VLC Plugin
  76.             int int_MilliSecondsTimeOut = (Session.Timeout * 60000) - 30000;
  77.             string options = "";
  78.             string str_Script = "";
  79.  
  80.             //Determine correct VLC Profile
  81.             int vlcProfile = GetVlcProfile();
  82.             VLCCommon VLC = new VLCCommon(vlcProfile);
  83.  
  84.             if (VLC.strmPlayer == "vlc")
  85.             {
  86.                 if (Request.ServerVariables["HTTP_USER_AGENT"].IndexOf("MSIE") > 0)
  87.                 {
  88.                     str_Script = @"
  89.                     <script>
  90.                         window.status = 'Play Stream Activated'
  91.                         function playVideo()
  92.                         {
  93.                         var options = new Array('http-caching=";
  94.                     str_Script += VLC.strmBuffer;
  95.                     str_Script += @"');
  96.                     ";
  97.                     string location = "document.video1.addTarget('http://" + serverAddr + "', options , 4+8, -666);";
  98.                     str_Script += location;
  99.                     str_Script += @"
  100.                                window.status = 'Play Button Clicked';
  101.                                document.video1.play();
  102.                         }
  103.                     </script>
  104.  
  105.                     ";
  106.                 }
  107.                 else
  108.                 {
  109.                     str_Script = @"
  110.                     <script>
  111.                         function playVideo()
  112.                         {
  113.                                document.video1.play();
  114.                         }
  115.                     </script>
  116.  
  117.                     ";
  118.                 }
  119.             }
  120.  
  121.             ClientScript.RegisterStartupScript(typeof(String), "playVideo", str_Script, false);
  122.         }
  123.  
  124.         public string getIP()
  125.         {
  126.             //Alternative method for obtaining external IP if it can not be obtained by Request.Servervariables
  127.             WebClient wclient = new WebClient();
  128.  
  129.             wclient.DownloadFile("http://www.whatismyip.com/", Request.MapPath("ip.txt"));
  130.  
  131.             FileStream FS = new FileStream(Request.MapPath("ip.txt"), FileMode.Open);
  132.             StreamReader SR = new StreamReader(FS);
  133.  
  134.             string entirefile = SR.ReadToEnd();
  135.             int startIndex = entirefile.IndexOf("<h1>Your IP");
  136.             int endIndex = entirefile.IndexOf("</", startIndex);
  137.  
  138.             string restOfTheLine = entirefile.Substring(startIndex + 15, (endIndex - startIndex - 15));
  139.             string theIp = restOfTheLine.TrimEnd("<>brh1 ".ToCharArray());
  140.             SR.Close();
  141.             FS.Close();
  142.  
  143.             File.Delete(Request.MapPath("ip.txt"));
  144.             return theIp.Trim();
  145.         }
  146.  
  147.         private void doPlay()
  148.         {
  149.             //Process to launch VLC on server to stream video
  150.  
  151.             string filename = GetFilename();
  152.  
  153.             //Develop list of arguments to be passed to VLC on the server
  154.             string args;
  155.  
  156.             //Instantiate a VLC instance defaulted to correct profile
  157.             int vlcProfile = GetVlcProfile();
  158.             VLCCommon VLC = new VLCCommon(vlcProfile);
  159.             
  160.             if (VLC.getStrmSetting() == "manual")
  161.             {
  162.                 if (VLC.getStrmVEncoder() == "h264")
  163.                 {
  164.                     //Instantiate H264 object to profile 1 (video)
  165.                     H264Encoder H264Enc = new H264Encoder(vlcProfile);
  166.                     args = VLC.genCommandLine(filename, VLC, H264Enc);
  167.                 }
  168.                 else
  169.                 {
  170.                     args = VLC.genCommandLine(filename, VLC);
  171.                 }
  172.             }
  173.             else
  174.             {
  175.                 args = VLC.genCommandLine(filename, VLC.getStrmSetting(),VLC.getStrmVideoSize(), VLC.getStrmMux(), VLC.getStrmBitRate(),
  176.                                           VLC.getStrmPort());
  177.             }
  178.             
  179.             //Log what is being started on the server
  180.             Logger.Info("***********************");
  181.             Logger.Info("VLC Path= " + VLC.strmVLCLoc);
  182.             Logger.Info("Args= " + args);
  183.  
  184.             //Build the process that will run VLC on the server
  185.             psi = new ProcessStartInfo(VLC.getVLCLoc() + "vlc.exe", args);
  186.             psi.UseShellExecute = false;
  187.             psi.RedirectStandardError = false;
  188.             psi.RedirectStandardInput = false;
  189.             psi.RedirectStandardOutput = false;
  190.  
  191.             //Start the VLC process on the server
  192.             proc = Process.Start(psi);
  193.             
  194.             //Set the priority of the started process based off of user configuration
  195.             switch(VLC.getStrmPriority().ToUpper())
  196.                 {
  197.                     case "BELOWNORMAL":
  198.                         proc.PriorityClass = ProcessPriorityClass.BelowNormal;
  199.                         break;
  200.                     case "ABOVENORMAL":
  201.                         proc.PriorityClass = ProcessPriorityClass.AboveNormal;
  202.                         break;
  203.                     case "NORMAL":
  204.                         proc.PriorityClass = ProcessPriorityClass.Normal;
  205.                         break;
  206.                     case "HIGH":
  207.                         proc.PriorityClass = ProcessPriorityClass.High;
  208.                         break;
  209.                     default:
  210.                         proc.PriorityClass = ProcessPriorityClass.Normal;
  211.                         break;
  212.                 }
  213.         }
  214.  
  215.         private string GetFilename()
  216.         {
  217.         //Setup schedule to obtain program info to stream
  218.             Schedule scheduleHelper = Global.Schedule;
  219.             string filename = "";
  220.  
  221.             //Search/pull the filename of the passed recording
  222.             if (Request.Params["rid"] != null)
  223.             {
  224.                 ScheduledRecording scheduledRecording = scheduleHelper.GetScheduledRecordingByOID(Convert.ToInt32(Request.Params["rid"]));
  225.                 if (scheduledRecording != null)
  226.                 {
  227.                     filename = scheduledRecording.getFileName();
  228.                 }
  229.             }
  230.             else
  231.             {
  232.                 //We are accessing either a Music of Video Library item
  233.                 if (Request.Params["mvid"] != null)
  234.                 {
  235.                     string path = (string)PublicDownload.Deseralize(Request.Params["mvid"]);
  236.                     filename = Library.GetRealPath(path);
  237.                     Logger.Info("***********************");
  238.                     Logger.Info("File Name Deserialized: " + filename);
  239.                 }
  240.                 else
  241.                 {
  242.                     //We are accessing an entire directory of music or videos
  243.                     if (Request.Params["mid"] != null)
  244.                     {
  245.                         string path = (string) PublicDownload.Deseralize((string)Request.Params["mid"]);
  246.                         string tmpDir = Environment.GetEnvironmentVariable("ALLUSERSPROFILE");
  247.                         filename = Path.Combine(tmpDir, Download.streamLibraryFolder(Request, path));
  248.                         Logger.Info("***********************");
  249.                         Logger.Info("File Location Deserialized: " + filename);
  250.                     }
  251.                 }
  252.             }
  253.             return filename;
  254.         }
  255.  
  256.         protected void extButton_Click(object sender, EventArgs e)
  257.         {
  258.             guideParams = Global.Settings;
  259.             string serverIP = Request.ServerVariables["http_host"];
  260.             int index = serverIP.IndexOf(":");
  261.             if (index > -1)
  262.                 serverIP = serverIP.Substring(0, index).Trim();
  263.             if ((serverIP == null) || (serverIP.Length < 1))
  264.                 serverIP = getIP();
  265.  
  266.             //Create the address the stream will be available on
  267.             serverAddr = serverIP + ":" + guideParams.strmPort;
  268.  
  269.             //Instantiate a VLC instance set to appropriate profile
  270.             int vlcProfile = GetVlcProfile();
  271.             VLCCommon VLC = new VLCCommon(vlcProfile);
  272.  
  273.             if (VLC.strmPlayer != "dnld")
  274.             {
  275.                 //Launch VLC External player
  276.                 if (VLC.strmPlayer == "vlc")
  277.                 {
  278.                     Response.AddHeader("Content-Type", "video/mpeg4");
  279.                     Response.AddHeader("Content-Disposition", "attachment; filename=\"stream.vlc\"");
  280.                     Response.Write("#EXTM3U\r\n");
  281.                     Response.Write("http://" + serverAddr + "\n");
  282.                     Response.End();
  283.                 }
  284.                 else
  285.                     //Launch Windows Media Player (as long as ASF is associated to Windows Media)
  286.                 {
  287.                     Response.AddHeader("Content-Type", "video/x-ms-asf");
  288.                     Response.AddHeader("Content-Disposition", "attachment; filename=\"stream.asx\"");
  289.                     Response.Write("http://" + serverAddr + "\n");
  290.                     Response.End();
  291.                 }
  292.             }
  293.             else
  294.             {
  295.                 //We are accessing either a Music of Video Library item.  We have seperate settings for each type.
  296.  
  297.                 Response.AddHeader("Content-Type", "text/plain");
  298.                 string filename = GetFilename();
  299.                 Response.AddHeader("Content-Disposition", "attachment; filename=\"download_stream.bat\"");
  300.                 string dnldFile = " --sout file/raw:\"%HOMEDRIVE%%HOMEPATH%\\" + filename.Substring(filename.LastIndexOf("\\") + 1, ((filename.Length - 1) - filename.LastIndexOf("\\")));
  301.                 
  302.                 //set file extension of downloaded file based on profile
  303.                 switch (vlcProfile)
  304.                 {
  305.                     //Profile 1 is the video profile
  306.                     case 1:
  307.                         switch (streamSetting.SelectedValue)
  308.                         {
  309.                             case "0":
  310.                                 dnldFile += ".mpg\"";
  311.                                 break;
  312.                             case "1":
  313.                                 dnldFile += ".wmv\"";
  314.                                 break;
  315.                             case "2":
  316.                                 dnldFile += ".wmv\"";
  317.                                 break;
  318.                             case "3":
  319.                                 dnldFile += ".mpg\"";
  320.                                 break;
  321.                             case "manual":
  322.                                 switch (VLC.strmVideo)
  323.                                 {
  324.                                     case "WMV1":
  325.                                         dnldFile += ".wmv\"";
  326.                                         break;
  327.                                     case "WMV2":
  328.                                         dnldFile += ".wmv\"";
  329.                                         break;
  330.                                     default:
  331.                                         dnldFile += ".mpg\"";
  332.                                         break;
  333.                                 }
  334.                                 break;
  335.                             default:
  336.                                 dnldFile += ".mpg\"";
  337.                                 break;
  338.                         }
  339.                         break;
  340.                     //Profile 2 is the audio profile
  341.                     case 2:
  342.                         switch (streamSetting.SelectedValue)
  343.                         {
  344.                             //since all the pre-config settings result in .mpg extension we only need to look at
  345.                             //the manaul setting
  346.                             case "manual":
  347.                                 switch (VLC.strmAudio)
  348.                                 {
  349.                                     case "mp4a":
  350.                                         dnldFile += ".mp4\"";
  351.                                         break;
  352.                                     case "mp3":
  353.                                         dnldFile += ".mp3\"";
  354.                                         break;
  355.                                     default:
  356.                                         dnldFile += ".mpg\"";
  357.                                         break;
  358.                                 }
  359.                                 break;
  360.                             default:
  361.                                 dnldFile += ".mpg\"";
  362.                                 break;
  363.                         }
  364.                         break;
  365.                     //Catch all.....we will handle the file as Profile 1 (video)
  366.                     default:
  367.                         switch (streamSetting.SelectedValue)
  368.                         {
  369.                             case "0":
  370.                                 dnldFile += ".mpg\"";
  371.                                 break;
  372.                             case "1":
  373.                                 dnldFile += ".wmv\"";
  374.                                 break;
  375.                             case "2":
  376.                                 dnldFile += ".wmv\"";
  377.                                 break;
  378.                             case "3":
  379.                                 dnldFile += ".mpg\"";
  380.                                 break;
  381.                             case "manual":
  382.                                 switch (VLC.strmVideo)
  383.                                 {
  384.                                     case "WMV1":
  385.                                         dnldFile += ".wmv\"";
  386.                                         break;
  387.                                     case "WMV2":
  388.                                         dnldFile += ".wmv\"";
  389.                                         break;
  390.                                     default:
  391.                                         dnldFile += ".mpg\"";
  392.                                         break;
  393.                                 }
  394.                                 break;
  395.                             default:
  396.                                 dnldFile += ".mpg\"";
  397.                                 break;
  398.                         }
  399.                         break;
  400.                 }
  401.                 //Response.Write("\"" + VLC.getVLCLoc() + "vlc.exe\" " + "http://" + serverAddr + " --sout file/raw:\"" + filename.Substring(filename.LastIndexOf("\\") + 1, ((filename.Length - 1) - filename.LastIndexOf("\\"))) + ".mpg\"");
  402.                 Response.Write("\"" + VLC.getVLCLoc() + "vlc.exe\" " + "http://" + serverAddr + dnldFile);
  403.                 Response.End();
  404.             }
  405.         }
  406.  
  407.         private static void killVLC()
  408.         {
  409.             // kill any existing VLC processes
  410.             try
  411.             {
  412.                 foreach (Process thisproc in Process.GetProcessesByName("vlc"))
  413.                 {
  414.                     thisproc.Kill();
  415.                 }
  416.             }
  417.             catch(Exception Exc)
  418.             {
  419.             }
  420.         }
  421.  
  422.         //Method that makes VLC on the server skip forward 10 seconds on the currently playing item
  423.         protected void skipForwardStream(object sender, EventArgs e)
  424.         {
  425.             StreamerPost strmPost = new StreamerPost();
  426.             strmPost.Url = "http://localhost:8080/";
  427.             strmPost.PostItems.Add("seek_value", "+10sec");
  428.             strmPost.PostItems.Add("control", "seek");
  429.             strmPost.Type = StreamerPost.PostTypeEnum.Get;
  430.             string result = strmPost.Post();
  431.             result = String.Empty;
  432.         }
  433.  
  434.         //Method that makes VLC on the server skip backward 10 seconds on the currently playing item
  435.         protected void skipBackwardStream(object sender, EventArgs e)
  436.         {
  437.             StreamerPost strmPost = new StreamerPost();
  438.             strmPost.Url = "http://localhost:8080/";
  439.             strmPost.PostItems.Add("seek_value", "-10sec");
  440.             strmPost.PostItems.Add("control", "seek");
  441.             strmPost.Type = StreamerPost.PostTypeEnum.Get;
  442.             string result = strmPost.Post();
  443.             result = String.Empty;
  444.         }
  445.  
  446.         //Method that makes VLC on the server skip to the next item in the playlist
  447.         protected void nextStream(object sender, EventArgs e)
  448.         {
  449.             StreamerPost strmPost = new StreamerPost();
  450.             strmPost.Url = "http://localhost:8080/";
  451.             strmPost.PostItems.Add("control","next");
  452.             strmPost.Type = StreamerPost.PostTypeEnum.Get;
  453.             string result = strmPost.Post();
  454.             result = String.Empty;
  455.         }
  456.  
  457.         //Method that makes VLC on the server skip back to the previous item in the playlist
  458.         protected void previousStream(object sender, EventArgs e)
  459.         {
  460.             StreamerPost strmPost = new StreamerPost();
  461.             strmPost.Url = "http://localhost:8080/";
  462.             strmPost.PostItems.Add("control", "previous");
  463.             strmPost.Type = StreamerPost.PostTypeEnum.Get;
  464.             string result = strmPost.Post();
  465.             result = String.Empty;
  466.         }
  467.  
  468.         //Method that makes VLC on the server stop playing
  469.         protected void stopStream(object sender, EventArgs e)
  470.         {
  471.             StreamerPost strmPost = new StreamerPost();
  472.             strmPost.Url = "http://localhost:8080/";
  473.             strmPost.PostItems.Add("control", "stop");
  474.             strmPost.Type = StreamerPost.PostTypeEnum.Get;
  475.             string result = strmPost.Post();
  476.             result = String.Empty;
  477.         }
  478.  
  479.         //Method that makes VLC on the server start to play the playlist
  480.         protected void playStream(object sender, EventArgs e)
  481.         {
  482.             StreamerPost strmPost = new StreamerPost();
  483.             strmPost.Url = "http://localhost:8080/";
  484.             strmPost.PostItems.Add("control", "play");
  485.             strmPost.Type = StreamerPost.PostTypeEnum.Get;
  486.             string result = strmPost.Post();
  487.             result = String.Empty;
  488.         }
  489.  
  490.         //Method that makes VLC on the server pause playing of the currently playing item
  491.         protected void pauseStream(object sender, EventArgs e)
  492.         {
  493.             StreamerPost strmPost = new StreamerPost();
  494.             strmPost.Url = "http://localhost:8080/";
  495.             strmPost.PostItems.Add("control", "pause");
  496.             strmPost.Type = StreamerPost.PostTypeEnum.Get;
  497.             string result = strmPost.Post();
  498.             result = String.Empty;
  499.         }
  500.  
  501.         //Method that Kills the VLC process on the server
  502.         protected void stopStreamer(object sender, EventArgs e)
  503.         {
  504.             //Kill the VLC process on the server
  505.             killVLC();
  506.  
  507.             //If we were streaming via the Stream Now option we need to get rid of the recording
  508.             if (Request.Params["rid"] != null && Request.Params["type"] != null)
  509.             {
  510.                 if (Request.Params["type"] == "sn")
  511.                 {
  512.                     Schedule scheduleHelper = Global.Schedule;
  513.                     ScheduledRecording scheduledRecording = scheduleHelper.GetScheduledRecordingByOID(Convert.ToInt32(Request.Params["rid"]));
  514.                     if (scheduledRecording != null)
  515.                     {
  516.                         scheduleHelper.CancelScheduledRecording(scheduledRecording);
  517.                         scheduledRecording = scheduleHelper.GetScheduledRecordingByOID(Convert.ToInt32(Request.Params["rid"]));
  518.                         scheduleHelper.CancelScheduledRecording(scheduledRecording);
  519.                     }
  520.                 }
  521.             }
  522.             
  523.             //Close the player window
  524.             ClientScript.RegisterStartupScript(typeof(String), "closeScript", "<script language=JavaScript>popupClose();</script>", false);
  525.         }
  526.  
  527.         protected void resetButton_Click(object sender, EventArgs e)
  528.         {
  529.             //Load settings
  530.             guideParams = Global.Settings;
  531.  
  532.             //Instantiate a VLC instance defaulted to correct profile
  533.             int vlcProfile = GetVlcProfile();
  534.             VLCCommon VLC = new VLCCommon(vlcProfile);
  535.  
  536.             //Web Streamer VLC Common Class
  537.             streamPlayer.Text = VLC.getPlayer();
  538.  
  539.             //Need to set the appropriate pre-configed options on the page based on player selection
  540.             streamPlayer_SelectedIndexChanged(null, null);
  541.  
  542.             externalPlayer.Checked = VLC.strmExternal;
  543.             streamPort.Text = VLC.getStrmPort().ToString();
  544.             streamBuffer.Text = VLC.getStrmBuffer().ToString();
  545.             streamPriority.Text = VLC.getStrmPriority();
  546.             streamBitRate.Text = VLC.getStrmBitRate().ToString();
  547.             streamVideoSizeOpt.SelectedValue = VLC.getStrmVideoSizeOpt();
  548.  
  549.             if (streamVideoSizeOpt.SelectedValue == "preset")
  550.             {
  551.                 streamVideoScaleSize.Visible = false;
  552.                 streamVideoSize.Visible = true;
  553.             }
  554.             else
  555.             {
  556.                 streamVideoSizeOpt.SelectedValue = "scale";
  557.                 streamVideoSize.Visible = false;
  558.                 streamVideoScaleSize.Visible = true;
  559.             }
  560.  
  561.             streamVideoSize.Text = VLC.getStrmVideoSize();
  562.             streamVideoScaleSize.Text = VLC.getStrmVideoScale();
  563.             streamSetting.SelectedValue = VLC.getStrmSetting();
  564.  
  565.             if (VLC.getStrmSetting() == "manual")
  566.             {
  567.                 preConfigMessage.Visible = false;
  568.                 manualOptions.Visible = true;
  569.  
  570.                 strmMux.SelectedValue = VLC.getStrmMux();
  571.                 strmVideo.SelectedValue = VLC.getStrmVideo();
  572.                 strmAudio.SelectedValue = VLC.getStrmAudio();
  573.                 strmAudoBitrate.SelectedValue = VLC.getStrmAudioBitRate();
  574.                 strmAudioChannels.SelectedValue = VLC.getStrmAudioChannels();
  575.                 strmVencoder.SelectedValue = VLC.getStrmVEncoder();
  576.                 if (VLC.getStrmVEncoder() == "h264")
  577.                 {
  578.                     H264.Visible = true;
  579.                     
  580.                     //Web Streamer H264 Video Encoder Class.  Instantiate to appropriate profile
  581.                     H264Encoder H264Enc = new H264Encoder(vlcProfile);
  582.  
  583.                     strmH264KeyInt.Text = H264Enc.getH264KeyInt();
  584.                     strmH264IDRInt.Text = H264Enc.getH264IDRInt();
  585.                     strmH264BFrames.Text = H264Enc.getH264BFrames();
  586.                     strmH264QuantizerParam.Text = H264Enc.getH264QuantizerParam();
  587.                     strmH264QuantizerMax.Text = H264Enc.getH264QuantizerMax();
  588.                     strmH264QuantizerMin.Text = H264Enc.getH264QuantizerMin();
  589.                     strmH264CABAC.Checked = H264Enc.getH264CABAC();
  590.                     strmH264LoopFilter.Checked = H264Enc.getH264LoopFilter();
  591.                     strmH264Analyse.Checked = H264Enc.getH264Analyse();
  592.                     strmH264FrameRef.Text = H264Enc.getH264FrameRef();
  593.                     strmH264Adapt.Text = H264Enc.getH264Adapt();
  594.                     strmH264Me.Text = H264Enc.getH264Me();
  595.                     strmH264SubME.Text = H264Enc.getH264SubME();
  596.                     strmH264ChromaME.Text = H264Enc.getH264ChromaME();
  597.                     strmH264MERange.Text = H264Enc.getH264MERange();
  598.                 }
  599.                 else
  600.                 {
  601.                     H264.Visible = false;
  602.                 }
  603.             }
  604.             else
  605.             {
  606.                 preConfigMessage.Visible = true;
  607.                 manualOptions.Visible = false;
  608.                 H264.Visible = false;
  609.             }
  610.         }
  611.  
  612.         private int GetVlcProfile()
  613.         {
  614.             //Determine if VLC object should be defualted to profile 1(video) or 2(audio)
  615.             int vlcProfile; 
  616.             if (Request.Params["playerType"] != null)
  617.             {
  618.                 vlcProfile = Convert.ToInt16(Request.Params["playerType"]);
  619.             }
  620.             else
  621.             {
  622.                 vlcProfile = 1;
  623.             }
  624.             return vlcProfile;
  625.         }
  626.  
  627.  
  628.         protected void startStreamButton_Click(object sender, EventArgs e)
  629.         {
  630.             StartStreamTable.Visible = false;
  631.             transConfigOptions.Visible = false;
  632.             buttonTable.Visible = true;
  633.  
  634.             hlSubmit_Click(null, null);
  635.             resetButton_Click(null, null);
  636.  
  637.             guideParams = Global.Settings;
  638.  
  639.             //Instantiate a VLC instance defaulted to correct profile
  640.             int vlcProfile = GetVlcProfile();
  641.             VLCCommon VLC = new VLCCommon(vlcProfile);
  642.  
  643.             //Get the server IP
  644.             string serverIP = Request.ServerVariables["http_host"];
  645.             int index = serverIP.IndexOf(":");
  646.             if (index > -1)
  647.                 serverIP = serverIP.Substring(0, index).Trim();
  648.             if ((serverIP == null) || (serverIP.Length < 1))
  649.                 serverIP = getIP();
  650.  
  651.             //Create the address the stream will be available on
  652.             serverAddr = serverIP + ":" + guideParams.strmPort;
  653.  
  654.             //Kill any running VLC processes
  655.             killVLC();
  656.  
  657.             //Start a keepalive routine so window does not timeout
  658.             AddKeepAlive();
  659.  
  660.             //Process to follow if user wants to use a browser plugin rather than an external player
  661.             if ((!VLC.strmExternal) && (VLC.strmPlayer != "dnld"))
  662.             {
  663.                 //Turn off the row of buttons for launching the player or killing the server stream
  664.                 buttonTable.Rows[0].Visible = false;
  665.                 //Turn on the row that contains the button for killing the server stream
  666.                 buttonTable.Rows[1].Visible = true;
  667.                 buttonTable.Rows[1].Cells[0].Visible = true;
  668.  
  669.  
  670.                 //If using VLC plugin provide user with controls to stop, play, pause skip back and forward
  671.                 if ((vlcProfile == 1 && guideParams.strmPlayer != "vlc") || (vlcProfile != 1 && guideParams.strmPlayer2 != "vlc"))
  672.                 {
  673.                     controlTable.Visible = false;
  674.                 }
  675.                 else
  676.                 {
  677.                     controlTable.Visible = true;
  678.                 }
  679.  
  680.                 //Set the parameter variables for the plugin players
  681.                 string vsize = VLC.strmVideoSize;
  682.  
  683.                 string width = " WIDTH=\"320\"";
  684.                 string heigth = " HEIGHTH=\"213\"";
  685.                 plugin.Text = "";
  686.                 if (VLC.strmPlayer != "dnld")
  687.                 {
  688.                     //Windows Media Plugin
  689.                     if (VLC.strmPlayer == "wmp")
  690.                     {
  691.                         plugin.Text = " <div align=\"center\">";
  692.                         plugin.Text = " <EMBED TYPE=\"application/x-mplayer2\"";
  693.                         plugin.Text += " AutoStart=\"false\"";
  694.                         plugin.Text += " NAME=\"video1\"";
  695.                         plugin.Text +=
  696.                             " ShowControls=\"1\" ShowStatusBar=\"1\" AutoSize=\"false\" loop=\"false\" EnableContextMenu=\"0\" DisplaySize=\"0\"";
  697.                         plugin.Text += " SRC=\"http://" + serverAddr + "\"";
  698.                         plugin.Text += width;
  699.                         plugin.Text += heigth;
  700.                         plugin.Text += " </EMBED>";
  701.                         plugin.Text += " </div>";
  702.                     }
  703.                     //VLC Plugin being displayed in IE
  704.                     else if (Request.ServerVariables["HTTP_USER_AGENT"].IndexOf("MSIE") > 0)
  705.                     {
  706.                         plugin.Text = " <div align=\"center\">";
  707.                         plugin.Text +=
  708.                             " <OBJECT id=\"video1\" codeBase=\"http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab\" " +
  709.                             width + " " + heigth +
  710.                             " classid=\"clsid:E23FE9C6-778E-49D4-B537-38FCDE4887D8\" events=\"True\" >";
  711.                         plugin.Text += " <PARAM NAME=\"AutoLoop\" VALUE=\"FALSE\">";
  712.                         plugin.Text += " <PARAM NAME=\"AutoPlay\" VALUE=\"FALSE\">";
  713.                         plugin.Text += " <PARAM NAME=\"MRL\" VALUE=\"\">";
  714.                         plugin.Text += " <PARAM NAME=\"ShowDisplay\" VALUE=\"TRUE\">";
  715.                         plugin.Text += " </OBJECT>";
  716.                         plugin.Text += " </div>";
  717.  
  718.                         //Add Client Script to play the video in IE
  719.                         AddPlayerScript();
  720.                     }
  721.                         //VLC Plugin being displayed in a non-IE browser
  722.                     else
  723.                     {
  724.                         plugin.Text = " <div align=\"center\">";
  725.                         plugin.Text +=
  726.                             " <embed type=\"application/x-vlc-plugin\" name=\"video1\" autoplay=\"no\" loop=\"no\" " +
  727.                             width + " " + heigth;
  728.                         plugin.Text += " target=\"http://" + serverAddr + "\">";
  729.                         plugin.Text += " </div>";
  730.                         //Add Client Script to play the video in non-IE browser
  731.                         AddPlayerScript();
  732.                     }
  733.                 }
  734.  
  735.                 //Set the user message telling them what to do
  736.                 streamMessage.Text = "Server is now streaming video.  Press Play to begin video.  WHEN DONE....Click button to stop the Server Stream.";
  737.             }
  738.             //Setup the page for external player usage
  739.             else
  740.             {
  741.                 //Turn off the control table since it is not needed
  742.                 controlTable.Visible = false;
  743.                 //Turn on the row of buttons for External Player Launch and Kill Server Process
  744.                 buttonTable.Rows[0].Visible = true;
  745.                 //Turn off the row that only contains the Kill Server Process button
  746.                 buttonTable.Rows[1].Visible = false;
  747.                 //Set the user message telling them what to do
  748.                 streamMessage.Text = "Server is now streaming video.  Launch external player to view stream.  WHEN DONE....Click button to stop the Server Stream.";
  749.  
  750.             }
  751.  
  752.             if (VLC.strmPlayer == "dnld")
  753.             {
  754.                 span3.InnerHtml = "Begin Download";
  755.                 if (vlcProfile == 1)
  756.                 {
  757.                     streamMessage.Text = "Server is now streaming video.  Launch download to save the streaming video.  WHEN DONE....Click button to stop the Server Stream.";  
  758.                 }
  759.                 else
  760.                 {
  761.                     streamMessage.Text = "Server is now streaming audio.  Launch download to save the streaming audio.  WHEN DONE....Click button to stop the Server Stream.";
  762.                 }
  763.             }
  764.  
  765.             // kill any existing VLC processes
  766.             killVLC();
  767.             //Start the stream on the server
  768.             doPlay();
  769.         }
  770.  
  771.         protected void playerOptsButton_Click(object sender, EventArgs e)
  772.         {
  773.             playerOptions.Visible = true;
  774.             transcodeOptions.Visible = false;
  775.             hlSubmit_Click(null, null);
  776.             resetButton_Click(null, null);
  777.         }
  778.  
  779.         protected void transOptsButton_Click(object sender, EventArgs e)
  780.         {
  781.             playerOptions.Visible = false;
  782.             transcodeOptions.Visible = true;
  783.             if (streamSetting.SelectedValue != "manual")
  784.             {
  785.                 manualOptions.Visible = false;
  786.             }
  787.             else
  788.             {
  789.                 manualOptions.Visible = true;
  790.             }
  791.             hlSubmit_Click(null, null);
  792.             resetButton_Click(null, null);
  793.         }
  794.  
  795.         protected void hlSubmit_Click(object sender, EventArgs e)
  796.         {
  797.             guideParams = Global.Settings;
  798.  
  799.             //Determine correct VLC Profile
  800.             int vlcProfile = GetVlcProfile();
  801.  
  802.             if (vlcProfile != 2)
  803.             {
  804.                 guideParams.strmPlayer = streamPlayer.Text;
  805.                 guideParams.strmExternal = externalPlayer.Checked;
  806.                 guideParams.strmPort = Convert.ToInt32(streamPort.Text);
  807.                 guideParams.strmBuffer = Convert.ToInt32(streamBuffer.Text);
  808.                 guideParams.strmPriority = streamPriority.Text;
  809.                 guideParams.strmBitRate = Convert.ToInt32(streamBitRate.Text);
  810.                 guideParams.strmVideoSizeOpt = streamVideoSizeOpt.Text;
  811.                 guideParams.strmVideoSize = streamVideoSize.Text;
  812.                 CultureInfo culture = new CultureInfo("en-US");
  813.                 guideParams.strmVideoScaleSize =
  814.                     String.Format("{0:00.00}",
  815.                                   decimal.Parse(streamVideoScaleSize.Text.Replace(',', '.'),
  816.                                                 NumberStyles.AllowDecimalPoint, culture)).Replace(
  817.                         ',', '.');
  818.                 streamVideoScaleSize.Text = guideParams.strmVideoScaleSize;
  819.  
  820.                 if (streamVideoSizeOpt.SelectedValue == "preset")
  821.                 {
  822.                     streamVideoScaleSize.Visible = false;
  823.                     streamVideoSize.Visible = true;
  824.                 }
  825.                 else
  826.                 {
  827.                     streamVideoSize.Visible = false;
  828.                     streamVideoScaleSize.Visible = true;
  829.                 }
  830.  
  831.                 guideParams.strmSetting = streamSetting.Text;
  832.  
  833.                 if (streamSetting.Text == "manual")
  834.                 {
  835.                     preConfigMessage.Visible = false;
  836.                     manualOptions.Visible = true;
  837.  
  838.                     guideParams.strmMux = strmMux.SelectedValue;
  839.                     guideParams.strmVideo = strmVideo.SelectedValue;
  840.                     guideParams.strmAudio = strmAudio.SelectedValue;
  841.                     guideParams.strmAudoBitrate = strmAudoBitrate.SelectedValue;
  842.                     guideParams.strmAudioChannels = strmAudioChannels.SelectedValue;
  843.                     guideParams.strmVEncoder = strmVencoder.SelectedValue;
  844.  
  845.                     if (strmVencoder.SelectedValue == "h264")
  846.                     {
  847.                         H264.Visible = true;
  848.                         guideParams.strmH264KeyInt = strmH264KeyInt.Text;
  849.                         guideParams.strmH264IDRInt = strmH264IDRInt.Text;
  850.                         guideParams.strmH264BFrames = strmH264BFrames.Text;
  851.                         guideParams.strmH264QuantizerParam = strmH264QuantizerParam.Text;
  852.                         guideParams.strmH264QuantizerMax = strmH264QuantizerMax.Text;
  853.                         guideParams.strmH264QuantizerMin = strmH264QuantizerMin.Text;
  854.                         guideParams.strmH264CABAC = strmH264CABAC.Checked;
  855.                         guideParams.strmH264LoopFilter = strmH264LoopFilter.Checked;
  856.                         guideParams.strmH264Analyse = strmH264Analyse.Checked;
  857.                         guideParams.strmH264FrameRef = strmH264FrameRef.Text;
  858.                         guideParams.strmH264Adapt = strmH264Adapt.Text;
  859.                         guideParams.strmH264Me = strmH264Me.Text;
  860.                         guideParams.strmH264SubME = strmH264SubME.Text;
  861.                         guideParams.strmH264ChromaME = strmH264ChromaME.Text;
  862.                         guideParams.strmH264MERange = strmH264MERange.Text;
  863.                     }
  864.                     else
  865.                     {
  866.                         H264.Visible = false;
  867.                     }
  868.                 }
  869.                 else
  870.                 {
  871.                     preConfigMessage.Visible = true;
  872.                     manualOptions.Visible = false;
  873.                     H264.Visible = false;
  874.                 }
  875.             }
  876.             else
  877.             {
  878.                 guideParams.strmPlayer2 = streamPlayer.Text;
  879.                 guideParams.strmExternal2 = externalPlayer.Checked;
  880.                 guideParams.strmPort2 = Convert.ToInt32(streamPort.Text);
  881.                 guideParams.strmBuffer2 = Convert.ToInt32(streamBuffer.Text);
  882.                 guideParams.strmPriority2 = streamPriority.Text;
  883.                 guideParams.strmBitRate2 = Convert.ToInt32(streamBitRate.Text);
  884.                 guideParams.strmVideoSizeOpt2 = streamVideoSizeOpt.Text;
  885.                 guideParams.strmVideoSize2 = streamVideoSize.Text;
  886.                 CultureInfo culture = new CultureInfo("en-US");
  887.                 guideParams.strmVideoScaleSize2 =
  888.                     String.Format("{0:00.00}",
  889.                                   decimal.Parse(streamVideoScaleSize.Text.Replace(',', '.'),
  890.                                                 NumberStyles.AllowDecimalPoint, culture)).Replace(
  891.                         ',', '.');
  892.                 streamVideoScaleSize.Text = guideParams.strmVideoScaleSize2;
  893.  
  894.                 if (streamVideoSizeOpt.SelectedValue == "preset")
  895.                 {
  896.                     streamVideoScaleSize.Visible = false;
  897.                     streamVideoSize.Visible = true;
  898.                 }
  899.                 else
  900.                 {
  901.                     streamVideoSize.Visible = false;
  902.                     streamVideoScaleSize.Visible = true;
  903.                 }
  904.  
  905.                 guideParams.strmSetting2 = streamSetting.Text;
  906.  
  907.                 if (streamSetting.Text == "manual")
  908.                 {
  909.                     preConfigMessage.Visible = false;
  910.                     manualOptions.Visible = true;
  911.  
  912.                     guideParams.strmMux2 = strmMux.SelectedValue;
  913.                     guideParams.strmVideo2 = strmVideo.SelectedValue;
  914.                     guideParams.strmAudio2 = strmAudio.SelectedValue;
  915.                     guideParams.strmAudoBitrate2 = strmAudoBitrate.SelectedValue;
  916.                     guideParams.strmAudioChannels2 = strmAudioChannels.SelectedValue;
  917.                     guideParams.strmVEncoder2 = strmVencoder.SelectedValue;
  918.  
  919.                     if (strmVencoder.SelectedValue == "h264")
  920.                     {
  921.                         H264.Visible = true;
  922.                         guideParams.strmH264KeyInt2 = strmH264KeyInt.Text;
  923.                         guideParams.strmH264IDRInt2 = strmH264IDRInt.Text;
  924.                         guideParams.strmH264BFrames2 = strmH264BFrames.Text;
  925.                         guideParams.strmH264QuantizerParam2 = strmH264QuantizerParam.Text;
  926.                         guideParams.strmH264QuantizerMax2 = strmH264QuantizerMax.Text;
  927.                         guideParams.strmH264QuantizerMin2 = strmH264QuantizerMin.Text;
  928.                         guideParams.strmH264CABAC2 = strmH264CABAC.Checked;
  929.                         guideParams.strmH264LoopFilter2 = strmH264LoopFilter.Checked;
  930.                         guideParams.strmH264Analyse2 = strmH264Analyse.Checked;
  931.                         guideParams.strmH264FrameRef2 = strmH264FrameRef.Text;
  932.                         guideParams.strmH264Adapt2 = strmH264Adapt.Text;
  933.                         guideParams.strmH264Me2 = strmH264Me.Text;
  934.                         guideParams.strmH264SubME2 = strmH264SubME.Text;
  935.                         guideParams.strmH264ChromaME2 = strmH264ChromaME.Text;
  936.                         guideParams.strmH264MERange2 = strmH264MERange.Text;
  937.                     }
  938.                     else
  939.                     {
  940.                         H264.Visible = false;
  941.                     }
  942.                 }
  943.                 else
  944.                 {
  945.                     preConfigMessage.Visible = true;
  946.                     manualOptions.Visible = false;
  947.                     H264.Visible = false;
  948.                 }
  949.             }
  950.  
  951.             Global.Settings.Save();
  952.  
  953.         }
  954.  
  955.         private void getTheme()
  956.         {
  957.             string theme = Convert.ToString(Session["theme"]);
  958.  
  959.             if (theme != null && theme != "")
  960.             {
  961.                 return;
  962.             }
  963.             else
  964.             {
  965.                 HttpCookie cookie = Request.Cookies["theme"];
  966.                 if (cookie != null && cookie.Value.Length > 0)
  967.                 {
  968.                     theme = cookie.Value;
  969.                 }
  970.                 else
  971.                 {
  972.                     theme = "Default";
  973.                 }
  974.                 Session["theme"] = "themes/" + theme;
  975.                 return;
  976.             }
  977.         }
  978.  
  979.         protected void streamPlayer_SelectedIndexChanged(object sender, EventArgs e)
  980.         {
  981.             if (IsPostBack)
  982.             {
  983.                 if (streamPlayer.SelectedValue == "dnld")
  984.                 {
  985.                     string holdSelection = streamSetting.SelectedValue;
  986.                     setDownloadTransOptions();
  987.  
  988.                     try
  989.                     {
  990.                         streamSetting.SelectedValue = holdSelection;
  991.                     }
  992.                     catch
  993.                     {
  994.                         streamSetting.Items[0].Selected = true;
  995.                     }
  996.  
  997.  
  998.                     hlSubmit_Click(null, null);
  999.                 }
  1000.                 else
  1001.                 {
  1002.                     string holdSelection = streamSetting.SelectedValue;
  1003.                     setNonDownloadTransOptions();
  1004.  
  1005.                     try
  1006.                     {
  1007.                         streamSetting.SelectedValue = holdSelection;
  1008.                     }
  1009.                     catch
  1010.                     {
  1011.                         streamSetting.Items[0].Selected = true;
  1012.                     }
  1013.  
  1014.                     hlSubmit_Click(null, null);
  1015.                 }
  1016.             }
  1017.         }
  1018.  
  1019.         private void setDownloadTransOptions()
  1020.         {
  1021.             streamSetting.Items.Clear();
  1022.             streamSetting.Items.Add("MP4V/MPGA/MPEG-TS");
  1023.             streamSetting.Items[0].Value = "dvs0";
  1024.             streamSetting.Items[0].Selected = true;
  1025.             streamSetting.Items.Add("MP2V/MPGA/MPEG-TS");
  1026.             streamSetting.Items[1].Value = "dvs1";
  1027.             streamSetting.Items.Add("MP1V/MPGA/MPEG-TS");
  1028.             streamSetting.Items[2].Value = "dvs2";
  1029.             streamSetting.Items.Add("Manual Options");
  1030.             streamSetting.Items[3].Value = "manual";
  1031.         }
  1032.  
  1033.         private void setNonDownloadTransOptions()
  1034.         {
  1035.             streamSetting.Items.Clear();
  1036.             streamSetting.Items.Add("MP4V/MP4A/MPEG-TS");
  1037.             streamSetting.Items[0].Value = "0";
  1038.             streamSetting.Items.Add("WMV/MP4A/MPEG-TS");
  1039.             streamSetting.Items[1].Value = "1";
  1040.             streamSetting.Items.Add("WMV/MP3/ASF");
  1041.             streamSetting.Items[2].Value = "2";
  1042.             streamSetting.Items.Add("H264/MP4A/MPEG-TS");
  1043.             streamSetting.Items[3].Value = "3";
  1044.             streamSetting.Items.Add("Manual Options");
  1045.             streamSetting.Items[4].Value = "manual";
  1046.         }
  1047.  
  1048.         protected void externalPlayer_CheckedChanged(object sender, EventArgs e)
  1049.         {
  1050.             if (IsPostBack)
  1051.             {
  1052.                 hlSubmit_Click(null, null);
  1053.                 resetButton_Click(null, null);
  1054.             }
  1055.         }
  1056. }
  1057. }
  1058.